<?php
namespace app\car\controller;
use app\AdminRest;

use app\car\model\ArticleCate;
use app\car\model\ArticleEvaluate;
use app\car\model\ArticleList;
use app\shop\model\IndexCompany;
use think\App;
use think\facade\Db;


class AdminArticle extends AdminRest
{

    protected $model;

    protected $cate_model;



    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new ArticleList();

        $this->cate_model  = new ArticleCate();


    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:分类列表
     */
    public function cateList(){

        $input = $this->_param;

        $dis[] = ['status','>',-1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->cate_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:分类下拉框
     */
    public function cateSelect(){

        $input = $this->_param;

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(isset($input['type'])){

            $dis[] = ['type','=',$input['type']];
        }

        $data = $this->cate_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 10:41
     * @功能说明:
     */
    public function getArticleCateType(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'type'    => 0

        ];

        $zero = $this->cate_model->where($dis)->where('status','>',-1)->find();

        $data['zero'] = !empty($zero)?1:0;

        $dis['type'] = 1;

        $one = $this->cate_model->where($dis)->where('status','>',-1)->find();

        $data['one'] = !empty($one)?1:0;

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:添加分类
     */
    public function cateAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        if(isset($input['type'])&&$input['type']!=2){

            $where[] = ['uniacid','=',$this->_uniacid];

            $where[] = ['status','>',-1];

            $where[] = ['type','=',$input['type']];

            $find = $this->cate_model->dataInfo($where);

            if(!empty($find)){

                $this->errorMsg('已经创建过该类型的分类了');
            }

        }

        $data = $this->cate_model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:编辑分类
     */
    public function cateUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        if(isset($input['type'])&&$input['type']!=2){

            $where[] = ['uniacid','=',$this->_uniacid];

            $where[] = ['status','>',-1];

            $where[] = ['type','=',$input['type']];

            $where[] = ['id','<>',$input['id']];

            $find = $this->cate_model->dataInfo($where);

            if(!empty($find)){

                $this->errorMsg('已经创建过该类型的分类了');
            }

        }

        $data = $this->cate_model->dataUpdate($dis,$input);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:分类详情
     */
    public function cateInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->cate_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:58
     * @功能说明:文章列表
     */
    public function articleList(){

        $input = $this->_param;

        if(isset($input['status'])){

            $dis[] = ['status','=',$input['status']];

        }else{

            $dis[] = ['status','>',-1];
        }

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:添加文章
     */
    public function articleAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $data = $this->model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:编辑文章
     */
    public function articleUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $data = $this->model->dataUpdate($dis,$input);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:文章详情
     */
    public function articleInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

      //  $data['label'] = !empty($data['label'])?array_column($data['label'],'title'):[];

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 10:06
     * @功能说明:文章评价列表
     */
    public function evaluateList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',-1];

        if(!empty($input['article_id'])){

            $dis[] = ['a.article_id','=',$input['article_id']];
        }

        if(!empty($input['user_name'])){

            $dis[] = ['b.user_num','like','%'.$input['user_name'].'%'];
        }

        $ev_model = new ArticleEvaluate();

        $data = $ev_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-02 15:24
     * @功能说明:评论修改
     */
    public function evaluateUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $ev_model = new ArticleEvaluate();

        $res  = $ev_model->dataUpdate($dis,$input);

        $data = $ev_model->dataInfo($dis);
        //删除减去评论数量
        if(isset($input['status'])&&$input['status']==-1){

            $res = $this->model->where(['id'=>$data['article_id']])->update(['ev_num'=>Db::raw('ev_num-1'),'total_num'=>Db::raw('total_num-1')]);

        }

        return $this->success($res);

    }













}
